package com.skku.selab.fl.ui.tool;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class ResultSetTableModel extends AbstractTableModel {
	protected Vector<String> columnHeaders;
	protected Vector<Vector<Object>> tableData;
	
	public ResultSetTableModel(ResultSet rset) throws SQLException {
		Vector<Object> rowData;
		ResultSetMetaData rsmd = rset.getMetaData();
		int count = rsmd.getColumnCount();
		columnHeaders = new Vector<String>(count);
		tableData = new Vector<Vector<Object>>();
		for (int i = 1; i <= count; i++) {
			columnHeaders.addElement(rsmd.getColumnName(i));
		}
		while (rset.next()) {
			rowData = new Vector<Object>(count);
			for (int i = 1; i <= count; i++) {
				rowData.addElement(rset.getObject(i));
			}
			tableData.addElement(rowData);
		}
		
		
	}
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnHeaders.size();
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return tableData.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		Vector<?> rowData = (Vector<?>) (tableData.elementAt(row));
	    return rowData.elementAt(column);
	}

	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public String getColumnName(int column) {
		return (String) (columnHeaders.elementAt(column));
	}
}
